package com.manage.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manage.demo.dao.model.StudentInfo;
import com.manage.demo.dao.model.ClassInfo;
import com.manage.demo.service.StudentService;

@Controller
public class studentController {
	@Autowired
	private StudentService StudentService;
	
	@RequestMapping(value="/student", method=RequestMethod.GET)
	public String login(HttpServletRequest request, Model model){
		List<String> Sessionlist = new ArrayList<String>();
		for(String row : StudentService.getSession()) {
			if(!Sessionlist.contains(row.substring(0, 4))) {
				Sessionlist.add(row.substring(0, 4));
			}
		};
		model.addAttribute("Session",Sessionlist);
		model.addAttribute("name", request.getSession().getAttribute("name"));
		model.addAttribute("perm", request.getSession().getAttribute("perm"));
		model.addAttribute("admin", request.getSession().getAttribute("admin"));
		System.out.println(model);
        return "/html/student";
	} 

	
	@RequestMapping(value="/studentsearch", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> search(HttpServletRequest request, @RequestParam(value = "session", required = true) String session, @RequestParam(value = "grade", required = true) String grade, @RequestParam(value = "classname", required = true) String classname, @RequestParam(value = "id", required = true) String id,@RequestParam(value = "limit", required = true) int limit, @RequestParam(value = "offset", required = true) int offset) {
		Map<String, Object> res = new HashMap<String, Object>();
		String user_input = (String) request.getSession().getAttribute("user_input");
		String perm = (String) request.getSession().getAttribute("perm");
		String admin = (String) request.getSession().getAttribute("admin");
		if(perm.equals("0")) {
            String newgrade = StudentService.getStudentListByCondition(session, grade, classname, user_input, 1, 0).get(0).getGrade();
            String newclassname = StudentService.getStudentListByCondition(session, grade, classname, user_input, 1, 0).get(0).getClass_num();
            res.put("data", StudentService.getStudentListByCondition("", newgrade, newclassname, id, limit, offset));
            res.put("count", StudentService.getCount("", newgrade, newclassname, id));
        }else{
        	res.put("data", StudentService.getStudentListByCondition(session, grade, classname,  id, limit, offset));
        	res.put("count", StudentService.getCount(session, grade, classname,  id));
        }
		
		return res;
	}
	
	@RequestMapping(value="/revisestudent", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> revisestudent(HttpServletRequest request, @RequestParam(value = "id", required = true) String id) {
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("data", StudentService.getStudentMange(id));
		return res;
	}
	
	@RequestMapping(value="/getid", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getid(HttpServletRequest request, @RequestParam(value = "id", required = true) Integer id) {
		Map<String, Object> res = new HashMap<String, Object>();
		List<Integer> idlist = new ArrayList<Integer>();
		String newid = "" + id;
		
		for(Integer item : StudentService.getStudentIdByCondition(id)) {
			idlist.add(item);
		};
		Integer size = idlist.size();
		if(size.equals(0)) {
			newid = newid.concat("001");
		}else {
			Integer newid1 = idlist.stream().max(Integer::compareTo).get();
			newid1 = newid1 + 1;
			newid = "" + newid1;
		}
		
		System.out.println(newid);
		res.put("newid", newid);
		return res;
	}
	
	@RequestMapping(value="/studentadd", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request, @RequestParam(value = "id", required = true) String id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "sex", required = true) String sex, @RequestParam(value = "ingrade", required = true) String ingrade, @RequestParam(value = "inclassname", required = true) String inclassname, @RequestParam(value = "tele", required = true) String tele, @RequestParam(value = "idcard", required = true) String idcard) {
		Map<String, Object> res = new HashMap<String, Object>();
		StudentService.setStudentByCondition(id, name, sex, ingrade, inclassname, tele, idcard);
		StudentService.setUserByCondition(id);
		
		res.put("code", "200");
		res.put("msg", "添加成功！");
		
		return res; 
	}
	
	@RequestMapping(value="/studentrevise", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> revise(HttpServletRequest request, @RequestParam(value = "id", required = true) String id, @RequestParam(value = "name", required = true) String name, @RequestParam(value = "sex", required = true) String sex, @RequestParam(value = "ingrade", required = true) String ingrade, @RequestParam(value = "inclassname", required = true) String inclassname, @RequestParam(value = "tele", required = true) String tele, @RequestParam(value = "idcard", required = true) String idcard) {
		Map<String, Object> res = new HashMap<String, Object>();
		Integer ret = StudentService.update(id, name, sex, ingrade, inclassname, tele, idcard);
		res.put("code", "200");
		res.put("msg", "修改成功！");
		res.put("data", ret);
		return res;
	}
	
	@RequestMapping(value="/studentdelete", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request,@RequestParam(value = "id", required = true) String id) {
		Map<String, Object> res = new HashMap<String, Object>();
		Integer ret = StudentService.delete(id);
		StudentService.deleteuser(id);
		res.put("code", "200");
		res.put("msg", "删除成功！");
		res.put("data", ret);
		return res;
	}
	
}
	
